<?php
namespace Admin\Controller;

class PrintController extends PublicController {
    function _initialize() {
        parent::_initialize ();
    }
    public function index()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $map = '';
            $storeId = 0;
        } else {
            $map['p.store_id'] = $store_id['store_id'];
            $storeId =  $store_id['store_id'];
        }
        $id =I("get.id");
        $config = M("printConfig")->where(array("id" => $id))->find();
        $store = M("store")->select();

        $this->assign ( "store", $store );
        $this->assign ( "storeId", $storeId );
        $this->assign ( "config", $config );
        $this->display();
    }

    /**
     * 获取列表
     */
    public function config_list()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $map = '';
        } else {
            $map['p.store_id'] = $store_id['store_id'];
        }
        $result = M("printConfig")->alias("p")->join("meal_store as s on p.store_id=s.store_id")->field("p.*,s.store_name")->where($map)->select();

        $this->assign ( "result", $result );
        $this->display();
    }

    /**
     * 设置配置
     */
    public function setconfig()
    {
        $data =I("post.");
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $data['store_id'] = I('post.store_id');
        } else {
            $data['store_id'] = $store_id['store_id'];
        }
        if($data['id']){
            //编辑
            $id = $data['id'];
            $data['modify_time'] = time();
            $data['modifier'] = $store_id['user_name'];
            $result = M("printConfig")->where("id='$id'")->save($data);
            if($result !== false){
                $this->success("修改配置成功！",U('Admin/Print/config_list'));
            }else{
                $this->error("操作失败");
            }
        } else {
            $data['create_time'] = time();
            $data['creater'] = $store_id['user_name'];
            $result = M('printConfig')->add($data);
            if($result !== false){
                $this->success("新增配置成功！",U('Admin/Print/config_list'));
            }else{
                $this->error("操作失败");
            }
        }
    }

    /**
     * 删除设置
     */
    public function del()
    {
        $id = I("get.id");
        $result = M("printConfig")->where(array(
            "id" => $id
        ))->delete();
        if ($result !== false) {
            $this->success("删除配置成功！");
        }else{
            $this->error("操作失败");
        }
    }

//_______________________________店铺管理____________________________________________

    /**
     * 店铺管理
     */
    public function storeList()
    {
        $result = M("store")->field("store_id,store_name,store_phone,creater,FROM_UNIXTIME(create_time,'%Y-%m-%d %H-%i-%s') as create_time,modifier,FROM_UNIXTIME(modify_time,'%Y-%m-%d %H-%i-%s') as modify_time,logo,bg_image")->select();
        $this->assign('result',$result);
        $this->display();
    }

    /**
     * 修改加载页面
     */
    public function store_add_edit()
    {
        $id = I('get.id');
        $data['store_id'] = $id;
        $dishes_category = M('store')->where($data)->find();
//        dump($dishes_category);die;
        $this->assign('result', $dishes_category);
        $this->display();
    }

    /**
     * 店铺编辑
     */
    public function editStore()
    {
        $id = I('post.store_id');
        $data['store_name'] = I('post.store_name');
        $data['store_phone'] = I('post.store_phone');
        $data['store_address'] = I('post.store_address');
        $data['taxpayer'] = I('post.taxpayer');
        $data['bank'] = I('post.bank');
        $data['bank_account'] = I('post.bank_account');
        $store_id = session("wadmin");
        //上传店铺logo
        if ($_FILES ['logo'] ['name'][0] !== '') {
            $files['logo'] = $_FILES ['logo'];
            $img1 = $this->upload($files);
            $picarr1 = array();
            foreach ($img1 as $key1 => $val1) {
                array_push($picarr1, ltrim($val1[savepath], ".") . $val1[savename]);
            }
            $data['logo'] = implode(',', $picarr1);
        }

        //上传店铺背景图
        if ($_FILES ['bg_image'] ['name'][0] !== '') {
            $files['bg_image'] = $_FILES ['bg_image'];
            $img1 = $this->upload($files);
            $picarr1 = array();
            foreach ($img1 as $key1 => $val1) {
                array_push($picarr1, ltrim($val1[savepath], ".") . $val1[savename]);
            }
            $data['bg_image'] = implode(',', $picarr1);
        }

        if ($id) {
            //修改
            $data['modifier'] = $store_id['user_name'];
            $data['modify_time'] = time();
            $result = M('store')->where("store_id='$id'")->save($data);
            if ($result === false) {
                $this->error("操作失败");
            } else {
                $this->success("操作成功",U('Admin/Print/storeList'));
            }
        } else {
            //新增
            $data['creater'] = $store_id['user_name'];
            $data['create_time'] = time();
            $result = M('store')->add($data);
            if ($result) {
                $this->success("操作成功",U('Admin/Print/storeList'));
            } else {
                $this->error("操作失败");
            }
        }
    }

    /**
     * 删除店铺
     */
    public function delStore()
    {
        //判断这个店铺下面是不是有数值
        $store_id = I('get.id');
        $result = M('dishes')->where("store_id='$store_id'")->find();

        if($result){
            $this->error("该店铺下面有商品不能删除!", U('Admin/Print/storeList'));
        }else{
            $result_delete = M('store')->delete($store_id);
            if ($result_delete) {
                $this->success("操作成功", U('Admin/Print/storeList'));
            } else {
                $this->error("删除失败", U('Admin/Print/storeList'));
            }
        }
    }
}